$(function()
{
    $(document).on('click','.search_list_type',function () {
        let type = $(this).data('value');
        $("#search_form").attr('action',type);
    });
    $('#banner').on('click', '.fiter_item', function()
    {
        let _thisChange = parseInt($(this).data('change')),
            _thisParentDataName = $(this).parents('.fiter_change').data('name');

        $('.search_tips_alert').hide();
        if (_thisParentDataName)
        {
            const formAction = $('#search_form').attr('action');
            if (_thisParentDataName === 'collection_type' && formAction === '/trade' && _thisChange === 1) {
                global_obj.div_mask();
                global_obj.win_alert_auto_close('暂时不能使用公司名称搜索海关记录', 'fail', 2000, undefined, 0, function()
                {
                    global_obj.div_mask(1);
                });
                return;
            }

            if (_thisParentDataName == 'collection_type' && ((_thisChange == 2 && zuoooo_config.Permit.ability.indexOf('hs_filter') == -1) || (_thisChange == 1 && zuoooo_config.Permit.ability.indexOf('company_filter') == -1)))
            {
                if (parseInt($('#AccountId').val()) == 0)
                {
                    themes_obj.loginBox('index_type');
                    return false
                }
                else
                {
                    themes_obj.upgradeBox('index_type');
                    $('.global_alert_upgrade .upgrade_level_box_new input[value=13]').parents('.level_item').click();
                    return false
                }
            }
            else
            {
                $('#banner .search').find(`input[name=${_thisParentDataName}]`).val(_thisChange);
                if ($(this).parents('.fiter_select').length)
                {
                    $('#banner .fiter_select').removeClass('show');
                    $('#banner .fiter_name').html($(this).text())
                }
                if (_thisParentDataName == 'collection_role')
                {
                    $(this).addClass('current').siblings().removeClass('current');
                    $('#banner .tuijian a.list_url').each(function()
                    {
                        let this_utl = $(this).data('url_' + _thisChange);
                        $(this).attr('href', this_utl);
                    });
                    $('#banner .title').html($('#banner .title').attr('data-tip' + _thisChange));
                    // 重置类型选择(按某某某搜索)
                    $.each($('.fiter_item.search_input_type'), function (index, item) {
                        if (index === 0) {
                            $(item).click();
                        }
                    });
                }
                else if (_thisParentDataName == 'collection_type')
                {
                    $(this).addClass('current').siblings().removeClass('current');
                    let _thisHtml = $(this).data('html');
                    if ($(window).width() <= 1024)
                    {
                        _thisHtml = _thisHtml.split('，')[0];
                    }
                    if (zuoooo_config.lang == 'en')
                    {
                        _thisHtml = lang_obj.trade.detail.p_choose + ' ' + $(this).text();
                        if ($(window).width() <= 1024)
                            _thisHtml = 'Enter keywords';
                    }
                    $('#banner .search form .input_box_content .fiter .fiter_select').each(function()
                    {
                        $(this).find("li").find("span").removeClass("fiter_select_li_choose").addClass("fiter_select_li_no_choose")
                    });
                    $(this).find("span").addClass("fiter_select_li_choose");
                    $('#banner .search form .input input').attr('placeholder', _thisHtml)
                }
            }
        }
    }).on('click', '.search .fiter .name', function(e)
    {
        if ($('#banner .fiter .name i').hasClass('turn'))
        {
            $('#banner .fiter_select').removeClass('show');
            $('#banner .fiter .name i').removeClass('turn')
        }
        else
        {
            $('#banner .fiter_select').addClass('show');
            $('#banner .fiter .name i').addClass('turn')
        }
    }).on('click', '.search_tips .s_t_btn', function()
    {
        if (!$(this).parents('.first_show_out').length)
        {
            $('.search_tips_alert').hide();
        }
    }).on('click', '.search_tips .close', function()
    {
        if (!$(this).parents('.first_show_out').length)
        {
            $('.search_tips_alert').hide()
        }
    });
    $(document).on('click', function(e)
    {
        let _thisTarget = e.target;
        if (!$(_thisTarget).hasClass('fiter_item') && !$(_thisTarget).hasClass('fiter_name') && !$(_thisTarget).hasClass('name') && !$(_thisTarget).parent().hasClass('name'))
        {
            $('#banner .fiter_select').removeClass('show');
            $('#banner .fiter .name i').removeClass('turn')
        }
    });
    // 搜索按钮点击
    // let dig_click;
    // $('#banner .search form .dig').click(function()
    // {
    //     if (parseInt($('#AccountId').val()) == 0)
    //     {
    //         themes_obj.loginBox('index_type');
    //         return false;
    //     }
    //     // themes_obj.recordUserModel('Search');
    //     var $this = $('#search_form');
    //     clearTimeout(dig_click);
    //     dig_click = setTimeout(function()
    //     {
    //         let f = ks = o = series = group = s = c = '';
    //         if ($this.find('input[name=collection_type]').val() != 1 && $this.find('input[name=Keywords]').val() != '')
    //         {
    //             let reg = /\s(co.,ltd|ltd|co.|co|corp.|c.v.|pte.|sdn.bhd|s.r.o.|s.r.l.|s.a.r.l.|s.p.a.|k.k.|y.k|a\/s|a.l|c.de.rl|edms.bpk|i.s|k.s|inc|bv|nv|sa|ag|mfy|gmbh|bhd|llp|plc|est|fzc|fzco|fze|llc|jsc|ojsc|ab|oy|pt|tbk|pte|pvt|pty|bd|ao|as|av|ca|cia|cie|cv|ets|ges|hb|indecom|is|kb|kg|kk|ltda|lte|mbh|mij|nl|npl)$/i;
    //             if (reg.test($this.find('input[name=Keywords]').val()))
    //             {
    //                 $('.search_tips_alert').show();
    //                 return false;
    //             }
    //         }
    //         if ($this.find('input[name=collection_type]').val() == 2 && $this.find('input[name=Keywords]').val() != '' && $this.find('input[name=Keywords]').val().length < 6)
    //         {
    //             global_obj.win_alert_auto_close(lang_obj.banner.hs_pla, 'fail', 2000, undefined, 0);
    //             return false;
    //         }
    //         if ($this.find('input[name=Keywords]').val() == '')
    //         {
    //             global_obj.new_win_alert(lang_obj.user.index.keyword_search, function()
    //             {
    //                 global_obj.div_mask(1);
    //             }, 'alert', 0, 'none');
    //             return false;
    //         }
    //         else if ($this.find('input[name=Keywords]').val() == '' && $('#filter_form').length == 0 && $this.find('input[name=Country\\[\\]]').length == 0)
    //         {
    //             global_obj.new_win_alert(lang_obj.user.index.keyword_search, function()
    //             {
    //                 global_obj.div_mask(1);
    //             }, 'alert', 0, 'none');
    //             return false;
    //         }
    //
    //
    //         // window.location.href = $this.attr('action') + '?' + $this.serialize();
    //         return true;
    //     }, 300);
    // });
    $('#search_form input[name=Keywords]').on('keydown', function(e)
    {
        if (e.keyCode == 13)
        {
            $('#banner .search form .dig').click();
        }
    });
    // 表单提交
    $('#search_form').submit(function()
    {
        if (parseInt($('#AccountId').val()) == 0)
        {
            themes_obj.loginBox('index_type');
            return false;
        }
        // themes_obj.recordUserModel('Search');
        var $this = $('#search_form');
        let f = ks = o = series = group = s = c = '';
        if ($this.find('input[name=collection_type]').val() != 1 && $this.find('input[name=Keywords]').val() != '')
        {
            let reg = /\s(co.,ltd|ltd|co.|co|corp.|c.v.|pte.|sdn.bhd|s.r.o.|s.r.l.|s.a.r.l.|s.p.a.|k.k.|y.k|a\/s|a.l|c.de.rl|edms.bpk|i.s|k.s|inc|bv|nv|sa|ag|mfy|gmbh|bhd|llp|plc|est|fzc|fzco|fze|llc|jsc|ojsc|ab|oy|pt|tbk|pte|pvt|pty|bd|ao|as|av|ca|cia|cie|cv|ets|ges|hb|indecom|is|kb|kg|kk|ltda|lte|mbh|mij|nl|npl)$/i;
            if (reg.test($this.find('input[name=Keywords]').val()))
            {
                $('.search_tips_alert').show();
                return false;
            }
        }
        if ($this.find('input[name=collection_type]').val() == 2 && $this.find('input[name=Keywords]').val() != '' && $this.find('input[name=Keywords]').val().length < 6)
        {
            global_obj.win_alert_auto_close(lang_obj.banner.hs_pla, 'fail', 2000, undefined, 0);
            return false;
        }
        if ($this.find('input[name=Keywords]').val() == '')
        {
            global_obj.new_win_alert(lang_obj.user.index.keyword_search, function()
            {
                global_obj.div_mask(1);
            }, 'alert', 0, 'none');
            return false;
        }
        else if ($this.find('input[name=Keywords]').val() == '' && $('#filter_form').length == 0 && $this.find('input[name=Country\\[\\]]').length == 0)
        {
            global_obj.new_win_alert(lang_obj.user.index.keyword_search, function()
            {
                global_obj.div_mask(1);
            }, 'alert', 0, 'none');
            return false;
        }
    });
    $('.main_poster .scrollDig,.main_cases .scrollDig').on('click', function()
    {
        $('body, html').animate({ scrollTop: 0 }, 500)
    });
    if ($(window).width() > 1024)
    {
        $(window).on('scroll', function()
        {
            let height = $(window).height();
            let scrollTop = $(window).scrollTop();
            $('.effectRise').each(function()
            {
                if ($(this).hasClass('fadeIn'))
                    return true;
                let top = $(this).offset().top;
                if (height + scrollTop > top)
                {
                    $(this).css({
                        transform: 'translateY(0)',
                        opacity: 1
                    }).addClass('fadeIn');
                    if ($('.effectRise').length == $('.effectRise.fadeIn').length)
                        $(window).off('scroll')
                }
            })
        });
        $('#banner .search .fiter .name').hover(function()
        {
            $('#banner .fiter_select').addClass('show');
            $('#banner .fiter .name i').addClass('turn')
        }, function()
        {
            $('#banner .fiter_select').removeClass('show');
            $('#banner .fiter .name i').removeClass('turn')
        });
        $('#banner .fiter_select').hover(function()
        {
            $('#banner .fiter_select').addClass('show');
            $('#banner .fiter .name i').addClass('turn')
        }, function()
        {
            $('#banner .fiter_select').removeClass('show');
            $('#banner .fiter .name i').removeClass('turn')
        })
    }
    else
    {
        $('.effectRise').css({
            transform: 'translateY(0)',
            opacity: 1
        });
        if (zuoooo_config.lang == 'en')
            $('#banner .search form .input input').attr('placeholder', 'Enter keywords')
    }
    // 热词点击
    $('#banner .tuijian a.list_keywords').on('click', function()
    {
        $('#banner .search form').append('<input type="hidden" name="Country[]" value="' + $(this).data('country') + '">');
        $('#banner .search form input[name=Keywords]').val($(this).data('value'));
        $('#banner .search form input[name=is_go]').val('1');
        $('#banner .search form .dig').click();
    });
    if ($('#product_alert').length)
    {
        $('#product_alert').ready(function()
        {
            // $.ajax({
            //     url: '/?do_action=action.saveProductFromIndexAlert',
            //     type: 'post',
            //     dataType: 'json',
            //     data: { role: 0 },
            //     success: function(result)
            //     {
            //         if (result.ret == 1)
            //         {
            //             $('.product_alert').show();
            //             global_obj.div_mask(0, 'pr_alert_index');
            //             $('#pr_alert_index').on("click", function()
            //             {
            //                 $('.product_alert').hide();
            //                 global_obj.div_mask(1, 'pr_alert_index')
            //             })
            //         }
            //     }
            // })
        });
    }
    $(".product_alert .close,.product_alert .btn,.product_alert .i_know").on('click', function(e)
    {
        let type = 1;
        if ($(this).hasClass('i_know') || $(this).hasClass('btn'))
        {
            type = 2;
        }
    });
    $('.user_login_box .close').on('click',function () {
        $('#div_mask').click();
    });

    $(document).on('ready',function () {
        // 加载推荐采购商
        $.ajax({
            url: ajax_request_domain + '/home/get_index_seller_list',
            method: 'get',
            beforeSend: (xhr) => {
                xhr.setRequestHeader("token", getLocalStorage('token'));
            },
            success: function(result) {
                if (result.code === 1)
                {
                    const ret = result.data;
                    // 后加载推荐采购商
                    let buyerListBox = $('.buyer_list_box');
                    // 各个国家下的列表
                    buyerListBox.children('.buyer_box_info_inner').each(function(index, item) {
                        const country = ($(item).attr('class').split(' '))[1];
                        const list = ret[country];
                        // 单列表子数据
                        $(item).children('.buyer_box_info').children('.buyer_item').each(function (k, elm) {
                            const row = list[k];
                            if (row) {
                                $(elm).removeClass('no_view');
                                $(elm).children('.b_img').css('background-image','url('+row.logo+')');
                                $(elm).children('.b_name').html(row.name);
                                $(elm).children('.b_time').children('.b_t_desc').html(row.date);
                                $(elm).attr('href', row.url);
                            } else {
                                $(elm).css('display','none');
                            }

                        });
                    });
                }
                if (result.code === 0)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close(result.msg, 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    });
                    return false;
                }
            },
            error: function (result) {
                if (result.status === 500)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close('System error, please try again later', 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    });
                    return false;
                }
                if (result.status === 401)
                {
                    themes_obj.loginBox('index_type');
                    return false;
                }
                else if (result.status  === 403)
                {
                    themes_obj.upgradeBox('index_type');
                    return false;
                }
            }
        });

        // 加载热搜关键词
        $.ajax({
            url: ajax_request_domain + '/base/get_hot_keywords',
            method: 'get',
            beforeSend: (xhr) => {
                xhr.setRequestHeader("token", getLocalStorage('token'));
            },
            success: function (result) {
                if (result.code === 1) {
                    const data = result.data;
                    let html = '';
                    $.each(data, function (index, item) {
                        html += '<div class="item">';
                        html += '   <div class="rank">' + index + '</div>';
                        html += '   <div class="title">' + item.keywords + '</div>';
                        html += '   <div class="rows blue">';
                        html += '       <a href="' + item.buyerSearchUrl + '">找采购商</a>';
                        html += '   </div>';
                        html += '   <div class="rows">';
                        html += '       <a href="' + item.supplierSearchUrl + '">找供应商</a>';
                        html += '   </div>';
                        html += '</div>';
                    });
                    $('.hot_keywords > .wide > .list').html(html);
                }
                if (result.code === 0)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close(result.msg, 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    });
                    return false;
                }
            },
            error: function (result) {
                if (result.status === 500)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close('System error, please try again later', 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    });
                    return false;
                }
                if (result.status === 401)
                {
                    themes_obj.loginBox('index_type');
                    return false;
                }
                else if (result.status  === 403)
                {
                    themes_obj.upgradeBox('index_type');
                    return false;
                }
            }
        });

        // 加载企业库
        $.ajax({
            url: ajax_request_domain + '/companies/get_enterprise_list',
            type:'post',
            dataType: 'json',
            data: {is_pagination: 0},
            beforeSend: (xhr) => {
                xhr.setRequestHeader("token", getLocalStorage('token'));
            },
            success: function (res) {
                let html = '';
                $.each(res.data, function (index, row) {
                    html += '<a class="supplier_item" href="' + row.url + '">';
                    html += '   <div class="b_img">';
                    html += '       <img src="' + row.logo + '" alt="">';
                    html += '   </div>';
                    html += '   <div class="b_info">';
                    html += '       <div class="b_name">' + row.name + '</div>';
                    html += '       <div class="b_country">';
                    html += '           <div class="i_out">';
                    html += '               <i class="icon_flag_middle flag_middle_' + row.country_code + '"></i>';
                    html += '           </div>';
                    html += '           <span>' + row.country_name + '</span>';
                    html += '       </div>';
                    html += '       <div class="b_website">';
                    html += '           <i class="iconfont icon-wangzhi"></i>' + row.website_url;
                    html += '       </div>';
                    html += '       <div class="b_contact">';
                    if (row.facebook_url)
                    {
                        html += '           <span><img src="/assets/site_resource/image/fackbook2.png" alt=""></span>';
                    }
                    else
                    {
                        html += '           <span><img src="/assets/site_resource/image/facebook.png" alt=""></span>';
                    }
                    if (row.twitter_url)
                    {
                        html += '           <span><img src="/assets/site_resource/image/twitter2.png" alt=""></span>';
                    }
                    else
                    {
                        html += '           <span><img src="/assets/site_resource/image/twitter.png" alt=""></span>';
                    }
                    if (row.linkedin_url)
                    {
                        html += '           <span><img src="/assets/site_resource/image/in2.png" alt=""></span>';
                    }
                    else
                    {
                        html += '           <span><img src="/assets/site_resource/image/in.png" alt=""></span>';
                    }
                    if (row.phone)
                    {
                        html += '           <span><img src="/assets/site_resource/image/phone2.png" alt=""></span>';
                    }
                    else
                    {
                        html += '           <span><img src="/assets/site_resource/image/phone.png" alt=""></span>';
                    }
                    if (row.email)
                    {
                        html += '           <span><img src="/assets/site_resource/image/email2.png" alt=""></span>';
                    }
                    else
                    {
                        html += '           <span><img src="/assets/site_resource/image/email.png" alt=""></span>';
                    }
                    if (row.website_url)
                    {
                        html += '           <span><img src="/assets/site_resource/image/expore2.png" alt=""></span>';
                    }
                    else
                    {
                        html += '           <span><img src="/assets/site_resource/image/expore.png" alt=""></span>';
                    }
                    html += '       </div>';
                    html += '   </div>';
                    html += '</a>';
                });
                $('.enterprise_list').html(html);
            },
            error: function (result) {
                if (result.status === 500)
                {
                    global_obj.div_mask();
                    global_obj.win_alert_auto_close('System error, please try again later', 'fail', 2000, undefined, 0, function()
                    {
                        global_obj.div_mask(1);
                    });
                    return false;
                }
                if (result.status === 401)
                {
                    themes_obj.loginBox('index_type');
                    return false;
                }
                else if (result.status  === 403)
                {
                    themes_obj.upgradeBox('index_type');
                    return false;
                }
            }
        });
    });
});
